package com.boruiimage.demo.utils;

import com.boruiimage.demo.doMain.Result;
import com.boruiimage.demo.repository.filePartDao;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public  class checkImages {
    public static Result checkImage(String imgNames) throws JpegProcessingException, IOException {
          Result rs = new Result();
          Map map = new HashMap();
          //分析图片
          String rootName = "/opt/bigdata_px_api/images/" + imgNames;
          //String rootName = "E:\\images\\" + imgNames;
          File img = new File(rootName);
          System.out.println("File Name:" + img);
          Metadata metadata = JpegMetadataReader.readMetadata(img);
          System.out.println("Directory Count: " + metadata.getDirectoryCount());
          //输出所有附加属性数据
          for (Directory directory : metadata.getDirectories()) {
              System.out.println("******\t" + directory.getName() + "\t******");
              for (com.drew.metadata.Tag tag : directory.getTags()) {
                  System.out.println(tag);

                  map.put(tag.getTagName(), tag.getDescription());
                  rs.setMap(map);
                 // System.out.println(map);
              }
          }
          String value = (String) map.get("Software");
          String data = (String) map.get("Date/Time");
          if (null != value && null != data) {
              if (value.indexOf("Adobe Photoshop") != -1) {
                  rs.setMsg("该图片不符合原始图片特征，有被Photoshop修改痕迹");
              } else if (value.indexOf("meitu") != -1) {
                  rs.setMsg("该图片不符合原始图片特征，有被美图修改痕迹");
              } else {
                  rs.setMsg("该图片不符合原始图片特征，有修改痕迹");
              }
          } else if (null != value && null == data) {
              if (value.indexOf("Adobe Photoshop") != -1) {
                  rs.setMsg("该图片不符合原始图片特征，有被Photoshop修改痕迹");
              } else if (value.indexOf("meitu") != -1) {
                  rs.setMsg("该图片不符合原始图片特征，有被美图修改痕迹");
              } else {
                  rs.setMsg("该图片不符合原始图片特征，有修改痕迹");
              }
          } else {
              rs.setMsg("该图片符合原始图片特征，没有修改痕迹");
          }
          return  rs;
    }
}